package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    //学员列表查询
    @GetMapping
    public Result list(StudentQueryParam studentQueryParam){
        log.info("学员列表查询:{}",studentQueryParam);
        PageResult<Student> studentPageResult = studentService.page(studentQueryParam);
        return Result.success(studentPageResult);
    }

    @Log
    //删除学员
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids){
        log.info("删除学员:{}", Arrays.toString(ids));
        studentService.delete(ids);
        return Result.success();
    }

    @Log
    //添加学员
    @PostMapping
    public Result add(@RequestBody Student student){
        log.info("添加学员");
        studentService.addStu(student);
        return Result.success();
    }

    //根据ID查询学员
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id){
        log.info("根据ID查询对应的学员:{}",id);
        Student student = studentService.findById(id);
        return Result.success(student);
    }

    @Log
    //修改学员
    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("修改学员:{}",student);
        studentService.update(student);
        return Result.success();
    }

    //违纪处理
    @PutMapping("/violation/{id}/{score}")
    public Result updateViolation(@PathVariable Integer id,@PathVariable Integer score){
        log.info("违纪处理的学员ID:{},扣除分数:{}",id,score);
        studentService.updateViolation(id,score);
        return Result.success();
    }
}
